import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home/home.vue'

Vue.use(VueRouter)

const routes = [
    {
        path:'/',
        redirect:'/home'
    },
    {
        path:'/home',
        name:'home',
        meta:{
            activeNum:0,
            isShowTabBar:true
        },
        component:Home,
        children:[
            {
                path:'/home/searchPopup',
                name:'searchPopup',
                component:()=>import('../views/SearchPopup/SearchPopup')
            }
        ]
    },
    {
        path:'/topic',
        name:'topic',
        meta:{
            activeNum:1,
            isShowTabBar:true
        },
        component:()=>import('../views/Topic/Topic.vue')
    },
    {
        path:'/category',
        name:'category',
        meta:{
            activeNum:2,
            isShowTabBar:true
        },
        component:()=>import('../views/Category/Category.vue')
    },
    {
        path:'/cart',
        name:'cart',
        meta:{
            activeNum:3,
            isShowTabBar:true
        },
        component:()=>import('../views/Cart/Cart.vue')
    },
    {
        path:'/user',
        name:'user',
        meta:{
            activeNum:4,
            isShowTabBar:true
        },
        component:()=>import('../views/User/User.vue')
    },
    {
        path:'/productDetail',
        name:'productDetail',
        component:()=>import('../views/ProductDetail/ProductDetail.vue')
    },
]
const router = new VueRouter({
    routes
})
router.beforeEach((to,from,next)=>{
    let token = localStorage.getItem('token')
    if(to.path=='/cart'){
        if(token){
            next()
            return
        }
        Vue.prototype.$toast('请先登录！')
        setTimeout(()=>{
            next('/user')
        },1000)
    }
    next() 
})
export default router
